package com.ap.core.dao.impl;

import com.ap.core.dao.SequenceGenerationDao;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.math.BigDecimal;

/**
 * Jpa class for the {@link SequenceGenerationDao}
 */
public class JpaSequenceGenerationDao implements SequenceGenerationDao {

    /** The entity manager. */
    protected EntityManager entityManager;

    /**
     * Sets the entity manager.
     *
     * @param pEntityManager
     *            the new entity manager
     */
    @PersistenceContext(unitName="ap-core")
    protected void setEntityManager(final EntityManager pEntityManager) {
        this.entityManager = pEntityManager;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Long getNextID(final String sql) {
        final BigDecimal result = (BigDecimal) entityManager.createNativeQuery(sql).getSingleResult();
        return result.longValue();
    }

}
